﻿using Models;
using Models.RModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedisHelper.Article
{
    public class Comment
    {
        /// <summary>
        /// 插入评论
        /// </summary>
        /// <param name="articleId">文章编号</param>
        /// <param name="r_Comment">评论对象</param>
        public bool InsertComment(int articleId, R_Comment r_Comment)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var index = rh.GetDatabase(3).ListRightPush(StaticConfigRidesName.ArticleComment_List(articleId), JsonConvert.SerializeObject(r_Comment));
                if (index != 0)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// 获取评论集合
        /// </summary>
        /// <param name="articleId">文章编号</param>
        /// <returns></returns>
        public List<R_Comment> GetCommentList(int articleId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var index = rh.GetDatabase(3).ListRange(StaticConfigRidesName.ArticleComment_List(articleId));
                List<R_Comment> list = new List<R_Comment>(index.Length);
                index.ToList().ForEach(u => list.Add(JsonConvert.DeserializeObject<R_Comment>(u)));
                return list;
            }
        }

        /// <summary>
        /// 获取文章评论数
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public long GetArticleCommentCount(int articleId) {
            using (RedisHelper rh = new RedisHelper()) {
                return rh.GetDatabase(3).ListLength(StaticConfigRidesName.ArticleComment_List(articleId));
            }
        }
    }
}
